<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title></title>
	<meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0,minimum-scale = 1.0,user-scalable = no,width = device-width">
	<meta name="format-detection" content="telephone=no" />
	<link rel="stylesheet" type="text/css" href="../csses/exchange.css">
</head>
<body>
	<div class="exchange_box">
		<div class="top">
			<span class="span1">总兑换股数</span>
			<span class="span2"></span>
		</div>
		<div class="regulation">
			<div class="regulationTitle">
				<img src="../image/regulation.png">
				<span>期权兑换规则</span>
			</div>
			<div class="regulationContent">
				1.非合伙人兑换：2个商币兑换1股期权；<br/>
				2.合伙人兑换：1个商币兑换1股期权；<br/>
				3.普通会员和VIP会员 没有兑换资格；<br/>
				4.金钻会员最多兑换100,000股；<br/>
				5.县级合伙人最多兑换1,000,000股；<br/>
				6.市级合伙人最多兑换5,000,000股；<br/>
				7.省级合伙人最多兑换10,000,000股；<br/>
				8.全国合伙人最多兑换50,000,000股；
			</div>
		</div>
		<button class="submitBtn1">立即兑换</button>
	</div>

	<div class="hiddenBox">
		<div class="buyBox">
			<img src="../image/close.png" class="close">
			<ul>
				<li>
					<span>购买股数</span>
					<input type="number" placeholder="请输入购买股数" class="number" oninput="if(value.length>8)value=value.slice(0,8)"></input>
				</li>
				<li>
					<span>支付密码</span>
					<input type="password" placeholder="请输入支付密码" class="password"></input>
				</li>
			</ul>
			<button class="submitBtn2">确认兑换</button>
		</div>

		<div class="remindBox">输入密码</div>
	</div>

	<script type="text/javascript" src="../lib/flexible.js"></script>
	<script type="text/javascript" src="../lib/jquery-1.10.1.min.js"></script>
	<script type="text/javascript">
		onload = function () {
			var cloudShares,source_detail,name,token,level,partnerLevel,isPartner;

			var userAgent = navigator.userAgent;
			//根据用户代理判断手机系统是Android还是ios
			if(userAgent.toLowerCase().indexOf("android") > -1){
				source_detail = "Android";
			} else {
				source_detail = "iOS";
			}

			if(source_detail == 'Android'){
				//js和android原生交互（webview）
				var result = JavaScriptInterface.getUserInfo();
				var obj = eval("("+result+")");//解析json字符串
				name = obj.userName;
			    token = obj.token;
			    get1(name,token);
				get2(name,token);
			}else if(source_detail == 'iOS'){
				function setupWebViewJavascriptBridge(callback) {
			        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
			        if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
			        window.WVJBCallbacks = [callback];
			        var WVJBIframe = document.createElement('iframe');
			        WVJBIframe.style.display = 'none';
			        WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
			        document.documentElement.appendChild(WVJBIframe);
			        setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
			    }

			    setupWebViewJavascriptBridge(function(bridge){
			    	// bridge.registerHandler('', function(data, responseCallback) {
		      //           var responseData = { 'Javascript Says':'Right back atcha!' }
		      //           //给OC的回调
		      //           responseCallback(responseData);
	       //      	});

	            	bridge.callHandler('registerJavascriptBridge',function(response) {
	            		name = response.userName;
	            		token = response.token;
	            		get1(name,token);
						get2(name,token);
	                });
			    });
			}
		
		

			$('.submitBtn1').click(function(){
				$('.hiddenBox').css('display','block');
			});

			$('.close').click(function(){
				$('.hiddenBox').css('display','none');
			});

			function remind(message){
				$('.remindBox').css('display','block').text(message);
				setTimeout(function(){
					$('.remindBox').css('display','none');
				},1500);
			}

			function get1(a,b){
				$.ajax({
					type : 'post',
					url : '/api/v1/user/info',
					data : {
						paramStr: "{'userName':'" + a + "','token':'" + b + "'}"
					},
					dataType : 'json',
					timeout : 10000,
					success : function(data){
						if(data.code == 200){
							var obj = JSON.parse(data.data);
							cloudShares = obj.cloudShares;
							level = obj.level;
							partnerLevel = obj.partnerLevel;
							isPartner = obj.isPartner;
							$('.span2').text(cloudShares);
						}
					},
					error : function (xhr,type,errorThrown) {
						//.....
					}
				});
			}

			function get2(c,d){
				$('.submitBtn2').click(function(){
					var $changeCloudShares = $('.number').val();
					var change = parseInt($changeCloudShares);
					var $payPassword = $('.password').val();
					if(!level || !partnerLevel || !isPartner || !userName || !token){
						remind('请检查网络');
					}else if($changeCloudShares % 100 !=0 || !$changeCloudShares || $changeCloudShares <= 0){
						remind('购买数量为100整数倍');
					}else if(!$payPassword){
						remind('请输入支付密码');
					}else{
						$.ajax({
							type : 'post',
							url : '/api/v1/cloud/cloudToShares',
							data : {
								paramStr : "{'userName':'" + c + "','token':'" + d + "','payPassword':'" + $payPassword + "','changeCloudShares':'" + $changeCloudShares + "'}"
							},
							dataType : 'json',
							timeout : 10000,
							success : function (data) {
								if(data.code == 200){
									$('.submitBtn2').css('disabled',true).css('background','#ccc');
									setTimeout(function(){
										location.reload();
									},1500);
								}
								remind(data.message);
							},
							error : function (xhr,type,errorThrown){
								//..........
							}
						});
					}
				});
			}
		}
	</script>
</body>
</html>